package com.milk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.milk.context.BaseContext;
import com.milk.entity.User;
import com.milk.entity.UserSignIn;
import com.milk.mapper.UserMapper;
import com.milk.mapper.UserSignInMapper;
import com.milk.result.Result;
import com.milk.service.IUserSignInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户签到表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class UserSignInServiceImpl extends ServiceImpl<UserSignInMapper, UserSignIn> implements IUserSignInService {

    @Autowired
    private UserSignInMapper userSignInMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户签到天数
     * @return
     */
    @GetMapping
    @ApiModelProperty("查询用户签到天数")
    public Integer signInDay() {
        UserSignIn userSignIn = userSignInMapper.selectById(BaseContext.getCurrentId());

        return userSignIn.getConsecutiveDays();
    }

    /**
     * 用户点击签到
     * @return
     */
    public String signIn() {
        //TODO 定时任务 每周一清除连续签到的天数

        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        //判断是否是连续签到
        UserSignIn userSignIn = userSignInMapper.selectById(BaseContext.getCurrentId());
        if (userSignIn != null) {
            LocalDateTime update = userSignIn.getUpdatetime();
            DayOfWeek updateOfWeek = update.plusDays(1).getDayOfWeek();

            if (dayOfWeek.equals(updateOfWeek)) {
                //增加连续签到的天数
                userSignIn.setConsecutiveDays(userSignIn.getConsecutiveDays() + 1);
                userSignIn.setUpdatetime(LocalDateTime.now());
                userSignInMapper.updateById(userSignIn);
            } else {
                //把连续签到的天数变为1
                userSignIn.setConsecutiveDays(1);
                userSignIn.setUpdatetime(LocalDateTime.now());
                userSignInMapper.updateById(userSignIn);
            }
            //判断签到的天数对应的奖励
            int reward = 0;
            //表示第一天签到给5积分 以后每签到多一天多给3积分
            reward = 5 + (userSignIn.getConsecutiveDays() - 1) * 3;
            String mgs = "签到成功，你已连续签到" + userSignIn.getConsecutiveDays()
                    + "天，恭喜你获得" + reward + "积分";
            //添加对应的奖励
            User user = userMapper.selectById(BaseContext.getCurrentId());
            user.setIntegral(user.getIntegral() + reward);
            userMapper.updateById(user);
            return mgs;
        }

            userSignIn = new UserSignIn();
            userSignIn.setUserId(BaseContext.getCurrentId());
            userSignIn.setUpdatetime(LocalDateTime.now());
            userSignIn.setConsecutiveDays(1);
            userSignInMapper.insert(userSignIn);
        return "签到成功，你已连续签到" + userSignIn.getConsecutiveDays()
                    + "天，恭喜你获得5积分";
    }

}
